<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1" import="org.apak.model.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Service order</title>
<script type="text/javascript"
	src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script type="text/javascript" src="./js/note.js"></script>
<script type="text/javascript" src="./apprisePlugin/apprise-1.5.full.js"></script>
<link rel="stylesheet" href="./apprisePlugin/apprise.css" type="text/css" />
<link rel="stylesheet" href="./css/design.css" type="text/css" />
</head>
<body>
<%
	String error = (String) request.getAttribute("error");
if(error == null)
	error = "";
			ServiceOrder serviceOrder = (ServiceOrder) request
					.getAttribute("serviceOrder");
			ServiceDevice[] serviceDeviceArray = (ServiceDevice[]) request.getAttribute("serviceDeviceArray");
			ServicePart[] servicePartArray = (ServicePart[]) request
					.getAttribute("servicePartArray");
			ServiceAction[] serviceActionArray = (ServiceAction[]) request
					.getAttribute("serviceActionArray");
			ServiceType[] serviceTypeArray = (ServiceType[]) request
					.getAttribute("serviceTypeArray");
			StatusType[] statusTypeArray = (StatusType[]) request
					.getAttribute("statusTypeArray");
			ServiceActionStatusType[] serviceActionStatusTypeArray = (ServiceActionStatusType[]) request
					.getAttribute("serviceActionStatusTypeArray");
			ServiceDeviceStatusType[] serviceDeviceStatusTypeArray = (ServiceDeviceStatusType[]) request
					.getAttribute("serviceDeviceStatusTypeArray");
		%>
		<a href="./serviceRequest?id=<%= serviceOrder.getService_request_fk() %>">...back to service request</a>
	<h2>Service order</h2>
	<form action="serviceOrder" method="post">
	<%
		if(serviceOrder.getSo_status_type_fk() == 1) {
	%>
		<input type="submit" name="actionButton" value="Add device" /><input
			type="submit" name="actionButton" value="Add work" /><input
			type="submit" name="actionButton" value="Add part" />
			<%
		}
			%>
<jsp:include page="employeeNote.jsp" />
<jsp:include page="customerNote.jsp" />
		<br /> Total sum:
		<%
			if (serviceOrder != null) {
		%>
		<%=serviceOrder.getPrice_total()%>
		<%
			}
		%>
		<br />
		___________________________________________________________________________________
		<h3>Service device list:</h3>
		<%
			if (serviceDeviceArray != null) {
		%>
		<table>
			<%
				for (int i = 0; i < serviceDeviceArray.length; i++) {
			%>
			<tr>
				<td><strong><%=serviceDeviceArray[i].getDeviceName()%></strong></td>
				<td><select disabled>
						<%
							if (serviceDeviceStatusTypeArray != null)
										for (int k = 0; k < serviceDeviceStatusTypeArray.length; k++) {
											ServiceDeviceStatusType sdst = serviceDeviceStatusTypeArray[k];
											if (serviceDeviceArray[i]
													.getService_device_status_type_fk() == sdst
													.getService_device_status_type()) {
						%>
						<option value="<%=sdst.getService_device_status_type()%>"
							selected><%=sdst.getType_name()%></option>
						<%
							} else {
						%>
						<option value="<%=sdst.getService_device_status_type()%>"><%=sdst.getType_name()%></option>
						<%
							}
										}
						%>
				</select></td>
				<td>
				<%
					if(serviceOrder.getSo_status_type_fk() == 1) {
				%>
					<button name="actionButton"
						value="changeDevice<%=serviceDeviceArray[i].getService_device()%>"
						type="submit">Change device</button> <%
 	if (serviceDeviceArray[i].getServiceActionArray().length == 0
 					&& serviceDeviceArray[i].getServicePartArray().length == 0) {
 %>
					<button name="actionButton"
						value="deleteDevice<%=serviceDeviceArray[i].getService_device()%>"
						type="submit">Delete device</button> <%
 	}
					}
 %>
				</td>
			</tr>
			<tr>
				<td colspan="3">
					<table>
						<%
							ServiceAction[] serviceActions = serviceDeviceArray[i]
											.getServiceActionArray();
									for (int j = 0; j < serviceActions.length; j++) {
						%>
						<tr>
							<td>work:</td>
							<td><input type="text"
								value="<%=serviceActions[j].getAction_description()%>"
								readonly></td>
							<td>type:</td>
							<td><select name="action_type" disabled>
									<%
										String unitTypeName = "";
													if (serviceTypeArray != null)
														for (int k = 0; k < serviceTypeArray.length; k++) {
															ServiceType serviceType = serviceTypeArray[k];
															if (serviceActions[j].getService_type_fk() == serviceType
																	.getService_type()) {
																unitTypeName = serviceType
																		.getService_unit_typeName();
									%>
									<option value="<%=serviceType.getService_type()%>" selected><%=serviceType.getType_name()%></option>
									<%
										} else {
									%>
									<option value="<%=serviceType.getService_type()%>"><%=serviceType.getType_name()%></option>
									<%
										}
														}
									%>
							</select></td>
							<td>status:</td>
							<td><select name="action_status" disabled>
									<%
										if (serviceActionStatusTypeArray != null)
														for (int k = 0; k < serviceActionStatusTypeArray.length; k++) {
															ServiceActionStatusType serviceActionStatusType = serviceActionStatusTypeArray[k];
															if (serviceActions[j]
																	.getService_action_status_type_fk() == serviceActionStatusType
																	.getService_action_status_type()) {
									%>
									<option
										value="<%=serviceActionStatusType
											.getService_action_status_type()%>"
										selected><%=serviceActionStatusType
											.getType_name()%></option>
									<%
										} else {
									%>
									<option
										value="<%=serviceActionStatusType
											.getService_action_status_type()%>"><%=serviceActionStatusType
											.getType_name()%></option>
									<%
										}
														}
									%>
							</select></td>
							<td>quantity:</td>
							<td><input type="text"
								value="<%=serviceActions[j].getService_amount()%>" readonly>[<%=unitTypeName%>]</td>
							<td>unit price:</td>
							<td><input type="text"
								value="<%=serviceActions[j].getPrice()%>" readonly></td>
							<td>total price:</td>
							<td><input type="text"
								value="<%=serviceActions[j].getService_amount()
								* serviceActions[j].getPrice()%>"></td>
							<td>
							<%
								if(serviceOrder.getSo_status_type_fk() == 1) {
							%>
								<button name="actionButton"
									value="changeAction<%=serviceActions[j].getService_action()%>"
									type="submit">Change work</button>
								<button name="actionButton"
									value="deleteAction<%=serviceActions[j].getService_action()%>"
									type="submit">Delete work</button>
							<%
								}
							%>
							</td>
						</tr>
						<%
							}
						%>
						<%
							ServicePart[] serviceParts = serviceDeviceArray[i]
											.getServicePartArray();
									for (int j = 0; j < serviceParts.length; j++) {
						%>
						<tr>
							<td>part:</td>
							<td colspan="5"><input type="text"
								value="<%=serviceParts[j].getPart_name()%>" readonly></td>
							<td>quantity:</td>
							<td><input type="text"
								value="<%=serviceParts[j].getPart_count()%>" readonly>[units]</td>
							<td>unit price:</td>
							<td><input type="text"
								value="<%=serviceParts[j].getPart_price()%>" readonly></td>
							<td>total price:</td>
							<td><input type="text"
								value="<%=serviceParts[j].getPart_count()
								* serviceParts[j].getPart_price()%>"
								readonly></td>
							<td>
							<%
								if(serviceOrder.getSo_status_type_fk() == 1) {
							%>
								<button name="actionButton"
									value="changePart<%=serviceParts[j].getService_part()%>"
									type="submit">Change part</button>
								<button name="actionButton"
									value="deletePart<%=serviceParts[j].getService_part()%>"
									type="submit">Delete part</button>
									<%
								}
									%>
							</td>
						</tr>
						<%
							}
						%>
					</table>
				</td>
			</tr>
			<%
				}
			%>
		</table>
		<%
			}
			if (serviceDeviceArray == null || serviceDeviceArray.length == 0) {
		%>
		empty<br>
		<%
			}
		%>
		___________________________________________________________________________________
		<h3>Additional work and parts:</h3>
		<table>
			<%
				if (serviceActionArray != null) {
					for (int i = 0; i < serviceActionArray.length; i++) {
			%>
			<tr>
				<td>work:</td>
				<td><input type="text"
					value="<%=serviceActionArray[i].getAction_description()%>"
					readonly></td>
				<td>type:</td>
				<td><select name="action_type" disabled>
						<%
							String unitTypeName = "";
									if (serviceTypeArray != null)
										for (int k = 0; k < serviceTypeArray.length; k++) {
											ServiceType serviceType = serviceTypeArray[k];
											if (serviceActionArray[i].getService_type_fk() == serviceType
													.getService_type()) {
												unitTypeName = serviceType
														.getService_unit_typeName();
						%>
						<option value="<%=serviceType.getService_type()%>" selected><%=serviceType.getType_name()%></option>
						<%
							} else {
						%>
						<option value="<%=serviceType.getService_type()%>"><%=serviceType.getType_name()%></option>
						<%
							}
										}
						%>
				</select></td>
				<td>status:</td>
				<td><select name="action_status" disabled>
						<%
							if (serviceActionStatusTypeArray != null)
										for (int k = 0; k < serviceActionStatusTypeArray.length; k++) {
											ServiceActionStatusType serviceActionStatusType = serviceActionStatusTypeArray[k];
											if (serviceActionArray[i]
													.getService_action_status_type_fk() == serviceActionStatusType
													.getService_action_status_type()) {
						%>
						<option
							value="<%=serviceActionStatusType
										.getService_action_status_type()%>"
							selected><%=serviceActionStatusType
										.getType_name()%></option>
						<%
							} else {
						%>
						<option
							value="<%=serviceActionStatusType
										.getService_action_status_type()%>"><%=serviceActionStatusType
										.getType_name()%></option>
						<%
							}
										}
						%>
				</select></td>
				<td>quantity:</td>
				<td><input type="text"
					value="<%=serviceActionArray[i].getService_amount()%>" readonly>[<%=unitTypeName%>]</td>
				<td>unit price:</td>
				<td><input type="text"
					value="<%=serviceActionArray[i].getPrice()%>" readonly></td>
				<td>total price:</td>
				<td><input type="text"
					value="<%=serviceActionArray[i].getService_amount()
							* serviceActionArray[i].getPrice()%>"></td>
				<td>
				<%
					if(serviceOrder.getSo_status_type_fk() == 1) {
				%>
					<button name="actionButton"
						value="changeAction<%=serviceActionArray[i].getService_action()%>"
						type="submit">Change work</button>
					<button name="actionButton"
						value="deleteAction<%=serviceActionArray[i].getService_action()%>"
						type="submit">Delete work</button>
						<%
					}
						%>
				</td>
			</tr>
			<%
				}

				}
				if (servicePartArray != null) {

					for (int i = 0; i < servicePartArray.length; i++) {
			%>
			<tr>
				<td>part:</td>
				<td colspan="5"><input type="text"
					value="<%=servicePartArray[i].getPart_name()%>" readonly></td>
				<td>quantity:</td>
				<td><input type="text"
					value="<%=servicePartArray[i].getPart_count()%>" readonly>[units]</td>
				<td>unit price:</td>
				<td><input type="text"
					value="<%=servicePartArray[i].getPart_price()%>" readonly></td>
				<td>total price:</td>
				<td><input type="text"
					value="<%=servicePartArray[i].getPart_count()
							* servicePartArray[i].getPart_price()%>"
					readonly></td>
				<td>
				<%
					if(serviceOrder.getSo_status_type_fk() == 1) {
				%>
					<button name="actionButton"
						value="changePart<%=servicePartArray[i].getService_part()%>"
						type="submit">Change part</button>
					<button name="actionButton"
						value="deletePart<%=servicePartArray[i].getService_part()%>"
						type="submit">Delete part</button>
						<%
					}
						%>
				</td>
			</tr>
			<%
				}
				}
				if ((servicePartArray == null || servicePartArray.length == 0)
						&& (serviceActionArray == null || serviceActionArray.length == 0)) {
			%>
			<tr>
				<td>empty</td>
			</tr>
			<%
				}
			%>
		</table>
		___________________________________________________________________________________
		<h3>Status:</h3>
		<select id="orderStatus" name="orderStatus">
			<%
				if (statusTypeArray != null && serviceOrder != null)
					for (int k = 0; k < statusTypeArray.length; k++) {
						StatusType statusType = statusTypeArray[k];
						if (serviceOrder.getSo_status_type_fk() == statusType
								.getSo_status_type()) {
			%>
			<option value="<%=statusType.getSo_status_type()%>" selected><%=statusType.getType_name()%></option>
			<%
				} else {
			%>
			<option value="<%=statusType.getSo_status_type()%>"><%=statusType.getType_name()%></option>
			<%
				}
					}
			%>
		</select> <input type="submit" name="actionButton" value="Save order" />
		<%
			if(serviceOrder.getSo_status_type_fk() == 1) {
		%>
		<input type="submit" id="invoiceBtn" name="actionButton" value="Invoice" disabled/>
		<%
			} else {
		%>
		<input type="submit" id="invoiceBtn" name="actionButton" value="Invoice" />
		<%
			}
		%>
		<%= error %>
	</form>
</body>
</html>